﻿Public Class ClassMatrice
    Private IndexLignes As New System.Collections.Generic.Dictionary(Of String, Long)
    Private IndexColonnes As New System.Collections.Generic.Dictionary(Of String, Long)
    Private Largeurs() As Long
    Private iColonnes As Long
    Private iLignes As Long
    Private Cellules() As ArrayList

    Public Sub New()
        Me.iColonnes = 0
        Me.iLignes = 0
        Me.IndexLignes.Clear()
        Me.IndexColonnes.Clear()
        Dim oLigne As New ArrayList()
        oLigne.Add("Path")
        ReDim Cellules(0)
        Cellules(0) = oLigne
        ReDim Largeurs(0)
        Largeurs(0) = 0
    End Sub

    Public ReadOnly Property Colonnes() As Long
        Get
            Return iColonnes
        End Get
    End Property

    Public ReadOnly Property Lignes() As Long
        Get
            Return iLignes
        End Get
    End Property

    Public Property Cellule(ByVal TitreLigne As String, ByVal TitreColonne As String) As String
        Get
            If Me.IndexLignes.ContainsKey(TitreLigne) And Me.IndexColonnes.ContainsKey(TitreColonne) Then
                Return Me.Cellules(Me.IndexLignes(TitreLigne)).Item(Me.IndexColonnes(TitreColonne))
            Else
                Return ""
            End If
        End Get
        Set(ByVal value As String)
            If Not Me.IndexLignes.ContainsKey(TitreLigne) Then Me.NouvelleLigne(TitreLigne)
            If Not Me.IndexColonnes.ContainsKey(TitreColonne) Then Me.NouvelleColonne(TitreColonne)
            Dim NoColonne As Long = Me.IndexColonnes(TitreColonne)
            Me.Cellules(Me.IndexLignes(TitreLigne)).Item(NoColonne) = value
            If value.Length > Me.Largeurs(NoColonne) Then Me.Largeurs(NoColonne) = value.Length
        End Set
    End Property

    Public Sub NouvelleLigne(ByVal Titre As String)
        If Not Me.IndexLignes.ContainsKey(Titre) Then
            Me.iLignes += 1
            Dim oLigne As New ArrayList()
            For iC As Long = 0 To iColonnes
                oLigne.Add("")
            Next
            oLigne.Item(0) = Titre
            ReDim Preserve Me.Cellules(iLignes)
            Me.Cellules(iLignes) = oLigne
            Me.IndexLignes.Add(Titre, iLignes)
            If Titre.Length > Largeurs(0) Then Largeurs(0) = Titre.Length
        End If
    End Sub

    Public Sub NouvelleColonne(ByVal Titre As String)
        If Not Me.IndexColonnes.ContainsKey(Titre) Then
            Me.iColonnes += 1
            For iL As Long = 0 To Me.iLignes
                Me.Cellules(iL).Add("")
            Next
            Me.Cellules(0).Item(iColonnes) = Titre
            Me.IndexColonnes.Add(Titre, iColonnes)
            ReDim Preserve Largeurs(iColonnes)
            Largeurs(iColonnes) = Titre.Length
        End If
    End Sub

    Public Function GetAsCSV() As String
        Dim sResult As String = ""
        Dim sLigne As String = ""
        For iL As Long = 0 To Me.iLignes
            sLigne = ""
            For iC As Long = 0 To Me.iColonnes
                If sLigne <> "" Then sLigne &= ","
                sLigne &= Me.Cellules(iL).Item(iC)
            Next
            If sResult <> "" Then sResult &= vbCrLf
            sResult &= sLigne
        Next
        Return sResult
    End Function

    Public Function GetAsText() As String
        Dim sResult As String = ""
        Dim sLigne As String = ""
        For iL As Long = 0 To Me.iLignes
            sLigne = ""
            For iC As Long = 0 To Me.iColonnes
                If sLigne <> "" Then sLigne &= "|"
                sLigne &= Me.Cellules(iL).Item(iC).ToString.PadRight(Largeurs(iC), " ")
            Next
            If sResult <> "" Then sResult &= vbCrLf
            sResult &= sLigne
        Next
        Return sResult
    End Function
End Class
